import api from "../../api/index.js";
Page({
  data: {
    nick: undefined,
    memberCard: {},
    todoThing:{
      todoPay:0,
      todoShopCart:0
    }
  },
  onLoad() {
    getApp().initLanguage(this)
    wx.setNavigationBarTitle({
      title: "我的",
    })

  },
  onShow() {
    let user = wx.getStorageSync('user')
    let memberCard = wx.getStorageSync('memberCard')
    this.setData({
      memberCard,
      user
    })
    // 待办事宜tab
    this.getTodoTab()
  },
  getTodoTab(){
    //购物车
    api.shoppingCartBill({
      method: "POST",
      data: {},
    }).then((res) => {
      let todoThing = this.data.todoThing
      todoThing.todoShopCart = res.data.sumCount
      this.setData({
        todoThing
      })
    })
    //待支付
  },
  //优惠券统计
  async couponStatistics() {
    const res = await WXAPI.couponStatistics(wx.getStorageSync('token'))
    if (res.code == 0) {
      this.setData({
        couponStatistics: res.data
      })
    }
  },
  scanOrderCode() {
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        wx.navigateTo({
          url: '/pages/order-details/scan-result?hxNumber=' + res.result,
        })
      },
      fail(err) {
        console.error(err)
        wx.showToast({
          title: err.errMsg,
          icon: 'none'
        })
      }
    })
  },
  navigateToCoupons() {
    wx.navigateTo({
      url: '/pages/coupons/index?activeIndex=1',
    })
  },
  goBalance() {
    wx.navigateTo({
      url: '/pages/asset/index',
    })
  },
  goScorelog() {
    wx.navigateTo({
      url: '/pages/score/logs',
    })
  },
  goadmin() {
    wx.navigateToMiniProgram({
      appId: 'wx5e5b0066c8d3f33d',
      path: 'pages/autoLogin?token=' + wx.getStorageSync('token'),
    })
  },
  clearStorage() {
    wx.clearStorageSync()
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  govip() {
    wx.navigateTo({
      url: '/pages/member-center/index',
    })
  },
  editNick() {
    this.setData({
      nickShow: true
    })
  },
  async _editNick() {
    if (!this.data.nick) {
      wx.showToast({
        title: this.data.$t.my.nickRequired,
        icon: 'none'
      })
      return
    }
    const postData = {
      token: wx.getStorageSync('token'),
      nick: this.data.nick,
    }
    // https://www.yuque.com/apifm/nu0f75/ykr2zr
    const res = await WXAPI.modifyUserInfoV2(postData)
    if (res.code != 0) {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
      return
    }
    wx.showToast({
      title: this.data.$t.common.submitSuccess,
    })
  },
  async onChooseAvatar(e) {
    console.log(e);
    const avatarUrl = e.detail.avatarUrl
  },
  goUserCode() {
    wx.navigateTo({
      url: '/pages/my/user-code',
    })
  },
  customerService() {
    wx.openCustomerServiceChat({
      extInfo: {
        url: wx.getStorageSync('customerServiceChatUrl')
      },
      corpId: wx.getStorageSync('customerServiceChatCorpId'),
      success: res => {},
      fail: err => {
        console.error(err)
      }
    })
  },
  copyuid() {
    wx.setClipboardData({
      data: this.data.apiUserInfoMap.base.id + ''
    })
  },
  navigateToBalance() {
    wx.navigateTo({
      url: '/pages/asset/index'
    });
  },

  onPointsClick() {
    wx.navigateTo({
      url: '/pages/points/points?status=1'
    });
  },

  onCouponClick() {
    wx.navigateTo({
      url: '/pages/coupons/coupons?status=1'
    });
  },
  // 处理头像更新
  handleChooseAvatar(avatarParam) {
    let avatarUrl = avatarParam.detail
    api.userUpdate({
      data: {
        avatarUrl
      }
    }).then((res) => {
      wx.setStorageSync('user', res.data);
      this.setData({
        user:res.data
      });
    });
  },

  // 处理昵称更新
  handleEditNick(nickNameParam) {
    let nickName = nickNameParam.detail
    if (!nickName) {
      wx.showToast({
        title: '更新失败，姓名不能为空',
        icon: 'none'
      });
      const user = {
        ...this.data.user
      };
      this.setData({
        user
      });
      return;
    }
    api.userUpdate({
      data: {
        nickName
      }
    }).then((res) => {
      wx.setStorageSync('user', res.data);
      this.setData({
        user:res.data
      });
    });
  },
  //待支付
  todoThing1(e) {
    api.orderPage({
      data: {
        payStatus: 10
      }
    }).then((res) => {
      wx.navigateTo({
        url: '/pages/order-details/index?id=' + res.data.records[0].id
      });
    })
  },
  todoThing2(e) {

  },
  todoThing3(e) {

  },
  todoThing4(e) {
    //跳转至购物车
    getApp().globalData.tabBar.shopCarStatus = true
    wx.switchTab({
      url: '/pages/order/index',
    })
  },
  selectshop() {
    wx.navigateTo({
      url: '/pages/company/index?modelType=1',
    })
  }
})